//
//  CategorySelectView.m
//  THiOSWeb
//
//  Created by songliu on 13-12-8.
//  Copyright (c) 2013年 Dexterlirush. All rights reserved.
//

#import "CategorySelectView.h"

@interface CategorySelectView ()
<
UITableViewDataSource,
UITableViewDelegate
>
@property (nonatomic, strong) IBOutlet UITableView *tableView;
@end

@implementation CategorySelectView

+ (CategorySelectView *)categorySelectView
{
    return [[[NSBundle mainBundle] loadNibNamed:@"CategorySelectView" owner:self options:nil] objectAtIndex:0];
}

- (void)awakeFromNib
{
    [self.tableView registerClass:[CategoryTableViewCell class] forCellReuseIdentifier:@"CategoryTableViewCell"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = YES;
    [self setSelectedRow:1];
}

- (void)setSelectedRow:(int)row
{
    _selectedRow = row;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 8;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CategoryTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"CategoryTableViewCell"];
    cell.bgImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"Pic2-04M%d.png", [indexPath row] + 1]];
    cell.selectedMark.hidden = indexPath.row != self.selectedRow;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 36;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectedRow = indexPath.row;
    if ([self.delegate respondsToSelector:@selector(categorySelectView:didSelectRow:)]) {
        [self.delegate categorySelectView:self didSelectRow:indexPath.row];
    }
    [self.tableView reloadData];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end


@implementation CategoryTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        _bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 0, 119, 30)];
        [self addSubview:self.bgImageView];
        self.backgroundView = nil;
        self.backgroundColor = [UIColor clearColor];
        
        _selectedMark = [[UIImageView alloc] initWithFrame:CGRectMake(250, 10, 19, 15)];
        _selectedMark.image = [UIImage imageNamed:@"Pic2-03"];
        [self addSubview:_selectedMark];
        _selectedMark.hidden = YES;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}
@end
